package TimeToWork.Android;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class TimePeriod implements Parcelable {

	private static final String TAG = "TimePeriod"; 
	
	private MyTime startTime = new MyTime(0);
	private MyTime endTime   = new MyTime(0);
	
	public TimePeriod() {
		
	}
	
	public TimePeriod(MyTime startTime, MyTime endTime) {
		this.startTime = startTime;
		this.endTime = endTime;
	}
	
	public TimePeriod(TimePeriod timePeriod) {
	    this.startTime = timePeriod.startTime.copy();
	    this.endTime   = timePeriod.endTime.copy();
	}
	
	public TimePeriod(Parcel in) {
	    this.startTime = in.readParcelable(MyTime.class.getClassLoader());
	    this.endTime = in.readParcelable(MyTime.class.getClassLoader());
	}
	
    public void writeToParcel(Parcel out, int flags) {
        out.writeParcelable(startTime, flags);
        out.writeParcelable(endTime, flags);
    }

    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<TimePeriod> CREATOR = new Parcelable.Creator<TimePeriod>() {
        public TimePeriod createFromParcel(Parcel in) {
            return new TimePeriod(in);
        }

        public TimePeriod[] newArray(int size) {
            return new TimePeriod[size];
        }
    };
	
	public TimePeriodLength getLength() {
		long timePeriodLength = 0;
		
		if ( (this.startTime.getTimeInMillis() == 0) || (this.endTime.getTimeInMillis() == 0) ) {
			Log.w(TAG, "Time period have not been initialized");
		}
		else {
			timePeriodLength = this.endTime.getTimeInMillis() - this.startTime.getTimeInMillis();
		}
		
		return new TimePeriodLength(timePeriodLength);
	}
	
	public MyTime getStartTime() {
	    return this.startTime;
	}
	
	public MyTime getEndTime() {
	    return this.endTime;
	}
	
	public void setStartTime(MyTime startTime) {
		this.startTime = startTime;
	}
	
	public void setEndTime(MyTime endTime) {
		this.endTime = endTime;
	}
	
	/** Returns whether the time period specified by this TimePeriod instance is within the time period specified by the parameter.
     * @param timePeriod the TimePeriod instance to compare
     * @return true when this TimePeriod is within timePeriod, false otherwise
     */
    public boolean inside(TimePeriod timePeriod) {
        Log.i(TAG, "Container " + timePeriod.startTime.getTimeInMillis() + " to " + timePeriod.endTime.getTimeInMillis());
        Log.i(TAG, "Timeperiod " + this.startTime.getTimeInMillis() + " to " + this.endTime.getTimeInMillis());
        if ( (timePeriod.startTime.getTimeInMillis() <= this.startTime.getTimeInMillis()) &&
             (timePeriod.endTime.getTimeInMillis() >= this.endTime.getTimeInMillis()) )  {
            return true;
        }
        else {
            return false;
        }           
    }
	
}
